<?php

namespace app\beautiful\model\goods;

use app\beautiful\model\order\OrderGoodsGroupDetailModel;
use app\common\model\CommonModel;

class GoodsWriteOffModel extends CommonModel{

    protected  $name = 'beautiful_goods_write_off';

    protected  $pk   = 'goods_write_off_id';
    /**
     * 模型写入前事件（钩子函数）
     */
    public static function onBeforeInsert($model)
    {
        $model->set("add_time", date('Y-m-d H:i:s'));
        $model->set("add_ip", request()->ip());
    }
    public function getById($where,$order='',$field='*')
    {
        $list = $this->where($where)->order($order)->field($field)->find();
        if($list) $list = $list->toArray();

        return $list;
    }

    public function getList($where,$order='',$page=0,$limit=0,$field='*'){
        $query = $this->where($where);
        $count = $query->count();
        $query = $query->order($order);
        if($page>0) $query = $query->page($page);
        if($limit>0) $query = $query->limit($limit);
        $list = $query->field($field)
            ->select()
            ->toArray();

        return compact('count','list');
    }

    public static function list($where,$order='',$page=0,$limit=0,$field='*'){
        $query = self::alias('o')
            ->leftjoin('account a','a.account_id=o.operate_id')
            ->leftjoin('beautiful_order_goods_group_detail d','o.order_goods_group_detail_id=d.order_goods_group_detail_id')
            ->where(['o.is_delete'=>0])
            ->when(isset($where['shop_id']),function ($query) use ($where){
                $query->where('o.shop_id','=',$where['shop_id']);
            })->when(isset($where['order_id']),function ($query) use ($where){
                $query->where('o.order_id','=',$where['order_id']);
            })->field($field);
        $count = $query->count();
        $query = $query->order($order);
        if($page>0) $query = $query->page($page);
        if($limit>0) $query = $query->limit($limit);
        $list = $query->select()->toArray();

        return compact('count','list');
    }

    public static function residueList($where,$order='',$page=0,$limit=0,$field='*'){
        $query = self::alias('o')
            ->leftjoin('beautiful_order_goods_group_detail d','o.order_goods_group_detail_id=d.order_goods_group_detail_id')
            ->where(['o.is_delete'=>0])
            ->when(isset($where['shop_id']),function ($query) use ($where){
                $query->where('o.shop_id','=',$where['shop_id']);
            })->when(isset($where['remain_num']),function ($query) use ($where){
                $query->where('o.remain_num','>',0);
            })->when(isset($where['order_id']),function ($query) use ($where){
                $query->where('o.order_id','=',$where['order_id']);
            })->when(isset($where['end_type']),function ($query) use ($where){
                $query->where('d.end_type','=',$where['end_type']);
            })->when(isset($where['over_ids']),function ($query) use ($where){
                $query->where('d.order_goods_group_detail_id','not in',$where['over_ids']);
            })->field($field);
        $count = $query->count();
        $query = $query->order($order);
        if($page>0) $query = $query->page($page);
        if($limit>0) $query = $query->limit($limit);
        $list = $query->group('o.order_goods_group_detail_id')->select()->toArray();
        return compact('count','list');
    }


    public function orderGoodsGroupDetail(){

        return  $this->hasOne(OrderGoodsGroupDetailModel::class,'order_goods_group_detail_id','order_goods_group_detail_id');

    }
}



